<template>
  <div class="complaint-submit">
    <div class="sub-page-header">
      <el-tooltip content="点击返回帮扶政策" placement="left">
        <div class="title" @click="goBack">
          <el-button icon="el-icon-arrow-left" type="text"></el-button>
          {{ formDisabled ? '政策详情' : id ? '修改帮扶政策' : '新增帮扶政策' }}
        </div>
      </el-tooltip>
    </div>
    <el-scrollbar class="apply-box submit-fixed">
      <div class="content">
        <div class="form">
          <el-form
            label-suffix="："
            :model="submitForm"
            :rules="rules"
            label-width="150px"
            ref="dataForm"
            :disabled="formDisabled">
            <div class="section-title syht14">基本信息</div>
            <el-form-item prop="policyName" label="政策名称">
              <el-input
                placeholder="请输入"
                v-model="submitForm.policyName"
                maxlength="120"
                show-word-limit
                clearable></el-input>
            </el-form-item>
            <el-form-item label="发文文号" prop="documentNumber">
              <el-input
                placeholder="请输入"
                v-model="submitForm.documentNumber"
                maxlength="50"
                show-word-limit
                clearable></el-input>
            </el-form-item>
            <el-form-item prop="suitEnterpriseTypeArr" label="适用企业类型">
              <el-select
                v-model="submitForm.suitEnterpriseTypeArr"
                placeholder="请选择"
                filterable
                popper-class="el-select-wrap"
                class="full_width"
                multiple>
                <el-option
                  v-for="item in enterpriseTypeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="policyTitleArr" label="政策主题">
              <el-select
                v-model="submitForm.policyTitleArr"
                placeholder="请选择"
                filterable
                popper-class="el-select-wrap"
                style="width: 100%"
                multiple>
                <el-option
                  v-for="item in policyTitleList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="policyContent" label="政策详情">
              <TinymceEditor v-model="submitForm.policyContent" height="400"></TinymceEditor>
            </el-form-item>
            <el-form-item prop="originalLink" label="原文链接">
              <el-input
                placeholder="请输入"
                v-model="submitForm.originalLink"
                maxlength="200"
                show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="附件信息" v-if="false">
              <UploadFile :file-list="albumList" />
            </el-form-item>
            <el-form-item prop="publishDate" label="发文日期">
              <el-date-picker
                class="full_width"
                v-model="submitForm.publishDate"
                type="date"
                placeholder="选择日期"></el-date-picker>
            </el-form-item>
          </el-form>
        </div>
        <div v-if="!formDisabled" class="btns-view">
          <div class="btn-box">
            <el-button class="btn-item" type="primary" @click="submitData">提交</el-button>
          </div>
        </div>
      </div>
    </el-scrollbar>
  </div>
</template>

<script>
import { getHelpPolicyDetailApi, editHelpPolicyApi, saveHelpPolicyApi } from '@api/modules/helpPolicy.js'
import UploadFile from '@com/UploadFile.vue'
import TinymceEditor from '@com/TinymceEditor.vue'
import { mapActions, mapGetters } from 'vuex'
import { getRegExp } from '@utility/validate.js'

export default {
  name: 'enterpriseCreate',
  components: { UploadFile, TinymceEditor },
  props: {},
  data() {
    return {
      id: '',
      albumList: [],
      submitForm: {
        policyName: '',
        documentNumber: '',
        originalLink: '',
        publishDate: '',
        policyContent: '',
        suitEnterpriseTypeArr: [],
        suitEnterpriseTypeList: [
          {
            relType: '',
            relTypeId: 0,
            orderBy: 0,
          },
        ],
        policyTitleArr: [],
        policyTitleList: [
          {
            relType: '',
            relTypeId: 0,
            orderBy: 0,
          },
        ],
      },
      rules: {
        policyName: [{ required: true, message: '请输入政策名称', trigger: 'blur' }],
        documentNumber: [{ required: true, message: '请输入发文文号', trigger: 'blur' }],
        suitEnterpriseTypeArr: [{ required: true, message: '请选择使用企业类型', trigger: 'blur' }],
        policyTitleArr: [{ required: true, message: '请选择政策主题', trigger: 'change' }],
        policyContent: [{ required: true, message: '请输入政策详情', trigger: 'blur' }],
        publishDate: [{ required: true, message: '请选择发文日期', trigger: 'blur' }],
      },
      policyTitleList: [],
      enterpriseTypeList: [],
    }
  },
  computed: {
    ...mapGetters('publicCode', ['getCode']),
    formDisabled() {
      return this.$route.params.view
    },
  },
  created() {
    this.getDictData()
    const { id } = this.$route.params
    this.id = id
    if (this.id) {
      this.getDetailData()
    }
  },
  methods: {
    ...mapActions('publicCode', ['getListDictData']),
    getDetailData() {
      getHelpPolicyDetailApi({ id: this.id }).then(res => {
        if (res.code === 0) {
          this.submitForm = { ...this.submitForm, ...res.data }
          const { policyTitleList, suitEnterpriseTypeList } = res.data
          this.submitForm.policyTitleArr = [...policyTitleList.map(item => item.relTypeId)]
          this.submitForm.suitEnterpriseTypeArr = [...suitEnterpriseTypeList.map(item => item.relTypeId)]
        }
      })
    },
    submitData() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          const reg = new RegExp(getRegExp('url'))
          if (this.submitForm.originalLink && !reg.test(this.submitForm.originalLink)) {
            this.$message.warning('原文链接格式不正确！')
            return
          }
          const params = { ...this.submitForm }

          params.suitEnterpriseTypeList = [
            ...this.dealDXlist(this.enterpriseTypeList, params.suitEnterpriseTypeArr),
          ].filter(item => item)

          params.policyTitleList = [...this.dealDXlist(this.policyTitleList, params.policyTitleArr)].filter(
            item => item
          )

          let API = null
          if (params.id) {
            API = editHelpPolicyApi
          } else {
            API = saveHelpPolicyApi
          }
          API(params).then(res => {
            if (res.code === 0) {
              this.$message.success(res.data)
              this.goBack()
            }
          })
        }
      })
    },
    dealDXlist(orginList, chooseArr) {
      let list = []
      list = orginList.map((item, index) => {
        let obj = null
        if (chooseArr.includes(item.id)) {
          obj = {
            id: '',
            orderBy: index + 1,
            relType: item.dictType,
            relTypeId: item.id,
            relTypeName: item.name,
          }
        }
        return JSON.parse(JSON.stringify(obj))
      })
      return list
    },
    // 重置表单
    resetForm() {
      if (this.$refs.dataForm) {
        this.$refs.dataForm.resetFields()
      }
    },
    async getDictData() {
      // 政策主题
      await this.getListDictData({ param: 'POLICY_TITLE' })
      // 企业类型
      await this.getListDictData({ param: 'ENTERPRISE_TYPE' })

      const { POLICY_TITLE, ENTERPRISE_TYPE } = this.getCode
      this.policyTitleList = [...POLICY_TITLE]
      this.enterpriseTypeList = [...ENTERPRISE_TYPE]
    },
    goBack() {
      this.$router.back(-1)
    },
  },
}
</script>

<style lang="scss" scoped>
.complaint-submit {
  height: 100%;
  @include flex-y;
  position: relative;
  background: #f5f6fa;
  padding: 20px;
  .el-scrollbar {
    @include flex-1;
  }
  ::v-deep .el-select-group {
    display: flex;
    flex-wrap: wrap;
  }

  .apply-box {
    background-color: #fff;
    .content {
      padding: 20px 30px;
    }
    .plus {
      cursor: pointer;
      font-size: 22px;
      margin-left: 10px;
    }
    .rules-select {
      display: flex;
    }
    .rule-oper {
      margin-right: 10px;
    }
    .section-title {
      padding: 10px;
      background: #f0f4f9;
      margin-bottom: 15px;
    }
    ::v-deep .form {
      .el-form-item {
        margin-bottom: 24px;
      }
    }
  }
  .btns-view {
    // @include flex-x(center, center);
    text-align: center;
    border-top: 1px solid $-border-color-base;
    margin-top: 10px;
    padding: 20px;
    background-color: #fff;
    .other-btn-box {
      margin-top: 10px;
      text-align: center;
    }
    .btn-item {
      width: 120px;
      height: 40px;
      font-size: 16px;
    }
    .empty {
      background-color: transparent;
      color: #4a79ba;
    }
  }
  .submit-fixed {
    padding-bottom: 100px;
    .btns-view {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
    }
  }

  @media screen and(max-width:800px) {
    .apply-box {
      .content {
        padding: 20px;
      }
    }
    .msg-container {
      .section-title {
        padding: 20px 50px;
        font-size: 36px;
        .icon-img-ts,
        .icon-img-person {
          display: inline-block;
          height: 36px;
          vertical-align: middle;
          margin-right: 10px;
          margin-top: -5px;
        }
      }
    }
    .el-input__count {
      font-size: 24px;
    }
  }
}
</style>
